# 购物系统1.0
# goods = {
#     "铅笔":2,
#     "橡皮":2,
#     "尺子":3,
#     "华为手表":804,
#     "小米手环":462,
#     "ipad11":3500,
#     "iphone16":5400,
#     "空调":6000
# }
#
# # 全局变量
# money = 3000
# shopping_car = []
#
# def shopping(str1):
#     global money,shopping_car
#     for item in str1:
#         if item in goods:
#             if goods[item] <= money:
#                 shopping_car.append(item)
#                 money -= goods[item]
#                 print("恭喜，购买成功！您的卡余额还剩{}".format(money))
#             else:
#                 print("穷鬼，钱不够！请到其他超市购买！")
#         else:
#             print("没有这个商品，别瞎搞！")
#
# str2 = ""
# while str2.lower() != "q":
#     str2 = input("请输入需要购买的商品（以空格分隔,输入q或Q退出）:")
#     if str2.lower() != "q":
#         items = str2.split()
#         shopping(items)
#
# print(shopping_car)
# print(money)



import random
from datetime import datetime

# 购物系统2.0
# 商品信息
goods = {
    "铅笔": 2,
    "橡皮": 2,
    "尺子": 3,
    "华为手表": 804,
    "小米手环": 462,
    "ipad11": 3500,
    "iphone16": 5400,
    "空调": 6000
}
# 优惠券信息
coupons = [
    {"商品": "空调", "折扣": 0.8, "描述": "空调8折优惠券"},
    {"商品": "iphone16", "折扣": 0.9, "描述": "iPhone16 9折优惠券"},
    {"商品": "ipad11", "折扣": 0.95, "描述": "iPad11 95折优惠券"},
]

# 全局变量
money = 3000  # 初始余额
shopping_car = []  # 购物车 {商品: 数量}
used_coupon = None  # 使用的优惠券
shopping_time = None  # 购物时间

# 抽取优惠券
def draw_coupon():
    global used_coupon
    used_coupon = random.choice(coupons)
    print(f"\n恭喜您抽到：{used_coupon['描述']}")

# 计算商品折扣后的价格
def calculate_discount(item, price):
    if used_coupon["商品"] == item:
        return price * used_coupon["折扣"]
    return price

# 计算购物车总价
def calculate_total():
    total = 0
    for item in shopping_car:
        price = goods[item]
        total += calculate_discount(item, price)
    return total

# 购物主流程
def shopping():
    global money, shopping_car, shopping_time

    while True:
        print("\n       欢迎购物       ")
        print(f"当前余额: {money}元")
        print(f"优惠券: {used_coupon['描述'] if used_coupon else '未使用'}")
        print("\n可选操作:")
        print("1. 购买商品")
        print("2. 退货")
        print("3. 结算")
        print("4. 退出")

        choice = input("\n请选择操作: ")

        if choice == "1":
            buy_goods()
        elif choice == "2":
            return_goods()
        elif choice == "3":
            checkout()
        elif choice == "4":
            print("感谢光临，欢迎下次再来！")
            break
        else:
            print("无效选择，请重新输入！")

# 购买商品
def buy_goods():
    global money, shopping_car

    print("\n 可购买的商品列表 ")
    for item, price in goods.items():
        # 显示原价和折后价
        discount_price = calculate_discount(item, price)
        if discount_price != price:
            print(f"{item}: {price}元 → {discount_price:.2f}元")
        else:
            print(f"{item}: {price}元")

    item = input("\n请输入要购买的商品名称(输入q返回): ")
    if item.lower() == "q":
        return

    if item not in goods:
        print("商品不存在，请重新选择！")
        return

    price = goods[item]
    discount_price = calculate_discount(item, price)

    if discount_price > money:
        print(f"余额不足！需要{discount_price}元，当前余额{money}元")
        return

    # 更新购物车和余额
    shopping_car.append(item)
    money -= discount_price
    print(f"购买成功！已购买{item}，已付款{discount_price:.2f}元")
    print(f"当前余额: {money}元")

# 退货功能
def return_goods():
    global money, shopping_car

    if not shopping_car:
        print("\n购物车为空，无法退货！")
        return

    item = input("\n请输入要退货的商品名称(输入q返回): ")
    if item.lower() == "q":
        return

    if item not in shopping_car:
        print("购物车中没有该商品！")
        return

    # 计算退款金额
    price = goods[item]
    discount_price = calculate_discount(item, price)

    # 更新购物车和余额
    shopping_car.remove(item)  # 只移除一个商品
    money += discount_price
    print(f"退货成功！已退回{item}，退款{discount_price:.2f}元")
    print(f"当前余额: {money}元")

# 结算并打印购物小票
def checkout():
    global money, shopping_car, shopping_time

    if not shopping_car:
        print("\n购物车为空，无法结算！")
        return

    # 计算总价
    total = calculate_total()

    if total > money:
        print(f"余额不足！需要{total}元，当前余额{money}元")
        return

    # 更新购物时间
    shopping_time = datetime.now()

    # 打印购物小票
    print("\n===== 购物小票 =====")
    print(f"时间: {shopping_time.strftime('%Y-%m-%d %H:%M:%S')}")
    print(f"优惠券: {used_coupon['描述']}")

    item_count = len(shopping_car)
    print(f"商品数量: {item_count}件")

    print("\n商品明细:")
    item_counts = {}
    for item in shopping_car:
        if item in item_counts:
            item_counts[item] += 1
        else:
            item_counts[item] = 1

    for item, count in item_counts.items():
        price = goods[item]
        discount_price = calculate_discount(item, price)
        print(f"{item} x {count}: {price}元/个 → {discount_price:.2f}元/个")

    print(f"\n原价总计: {sum(goods[item] for item in shopping_car)}元")
    print(f"折扣后总计: {total:.2f}元")
    print(f"卡余额: {money - total:.2f}元")

    # 清空购物车
    money -= total
    shopping_car = []
    print("\n结算成功！感谢您的购物！")

print("请先抽取优惠券再购物！")

# 抽取优惠券
draw_coupon()

# 开始购物
shopping()

